package com.gonsin.project.creator.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Scanner;

public class CmdUtils {

    /**
     * 读取命令行
     * @return
     */
    public static Object readCmd(String message, boolean nullable){
        String out = null;
        do {
            System.out.println(message);
            Scanner scanner = new Scanner(System.in, "GBK");
            out = scanner.nextLine();
//            System.out.println("out = " + out.length());
//            try {
//                FileUtils.writeByteArrayToFile(new File("t"), out.getBytes());
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//            StringBuffer sb = new StringBuffer();
//            BufferedReader br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
//            try {
//                out = br.readLine();
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }


//            char c = 0;
//            do {
//                try {
//                    c = (char) br.read();
//                    if(c != '\n'){
//                        sb.append(c);
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            } while (c != '\n');
//            out = sb.toString();
        } while ("".equals(out) && !nullable);
        out = out.trim();
        if("y".equalsIgnoreCase(out)){
            return true;
        }
        if("true".equalsIgnoreCase(out)){
            return true;
        }
        if("n".equalsIgnoreCase(out)){
            return false;
        }
        if("false".equalsIgnoreCase(out)){
            return false;
        }
        return out;
    }


    /**
     * 读取成true/false
     * @param message
     * @return
     */
    public static boolean readBoolean(String message, boolean defaultValue){
        String out = null;
        do {
            System.out.println(message);
            StringBuffer sb = new StringBuffer();
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            char c = 0;
            do {
                try {
                    c = (char) br.read();
                    if(c != '\n'){
                        sb.append(c);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } while (c != '\n');
            out = sb.toString();
            if(!Arrays.asList("Y", "y", "N", "n", "").contains(out)){
                System.out.println("无效输入，必须是 \"Y\", \"y\", \"N\", \"n\", \"\" 其中之一");
            } else {
                break;
            }
        } while (true);

        if(StringUtils.isEmpty(out)){
            return defaultValue;
        }

        out = out.equalsIgnoreCase("y") ? "true" : "false";

        return BooleanUtils.toBoolean(out);
    }


    /**
     * args 内是否包含 content
     */
    public static boolean contain(String[] args, String content) {
        return ArrayUtils.contains(args, content);
    }
}
